﻿using System;
using System.Threading.Tasks;

namespace BaseSyntax.ThreadTest
{
	public class TaskContinue:ITest
	{
		public const int RepeatNum = 1000;
		
		public void Test()
		{
			// Task task = Task.Run(TaskRun1).ContinueWith(TaskRun2).ContinueWith(TaskRun3);
			// Console.WriteLine("task1 " + task.Id);
			// task.Wait();
			
			Console.WriteLine("Before");
			// Use Task.Factory.StartNew<string>() for
			// TPL prior to .NET 4.5
			Task taskA = Task.Run(
					() => Console.WriteLine("Starting...")
				)
				.ContinueWith(antecedent =>
					Console.WriteLine("Continuing A..."));
			Task taskB = taskA.ContinueWith(antecedent =>
				Console.WriteLine("Continuing B..."));
			Task taskC = taskB.ContinueWith(antecedent =>
				Console.WriteLine("Continuing C..."));
			Task.WaitAll(taskC);
			Console.WriteLine("Finished!");
		}

		void TaskRun1()
		{
			Console.WriteLine("taskrun1");
			for (int i = 0; i < RepeatNum; i++)
			{
				Console.Write("+");
			}
		}

		void TaskRun2(Task task)
		{
			Console.WriteLine(task.Id);
			Console.WriteLine("taskrun2");
			for (int i = 0; i < RepeatNum; i++)
			{
				Console.Write("-");
			}
		}

		void TaskRun3(Task task)
		{
			Console.WriteLine(task.Id);
			Console.WriteLine("taskrun3");
			for (int i = 0; i < RepeatNum; i++)
			{
				Console.Write("/");
			}
		}
	}
}